package com.example.attendancesystem.Mapper;



import com.example.attendancesystem.Models.LeaveRow;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface LeaveRowMapper {

    /**
     * 添加一条请假记录
     *
     * @param leaveRow eaveRow
     * @return 操作受影响的记录数
     */
    @Insert("insert into leave_row (emp_number,emp_name,department,emp_position,leave_date,leave_time,reason,read_flag,check_flag,agree_flag)" +
            "value (#{leaveRow.empNumber},#{leaveRow.empName},#{leaveRow.department},#{leaveRow.empPosition},#{leaveRow.leaveDate},#{leaveRow.leaveTime},#{leaveRow.reason},#{leaveRow.readFlag},#{leaveRow.checkFlag},#{leaveRow.agreeFlag})")
    int addEmpLeaveRequest(@Param("leaveRow") LeaveRow leaveRow);


    /**
     * 获取未处理员工的请假请求
     *
     * @return LeaveRow列表
     */
    @Select("select id,emp_number,emp_name,department,emp_position,leave_date,leave_time,reason,read_flag,check_flag,agree_flag from leave_row where check_flag = 2")
    List<LeaveRow> getEmpLeaveRequest();

    /**
     * 获取未处理请假数目
     *
     * @return sum数目
     */
    @Select("select COUNT(id) from leave_row where check_flag = 2")
    Integer getLeaveRequestSum();

    /**
     * 获取所有的请假记录
     *
     * @return LeaveRow列表
     */
    @Select("select id,emp_number,emp_name,department,emp_position,leave_date,leave_time,reason,read_flag,check_flag,agree_flag from leave_row")
    List<LeaveRow> getAllLeaveRow();


    /**
     * 根据日期查找请假记录
     *
     * @param queryDate 查询的日期
     * @return LeaveRow列表
     */
    @Select("select id,emp_number,emp_name,department,emp_position,leave_date,leave_time,reason,read_flag,check_flag,agree_flag from leave_row where leave_date = #{queryDate}")
    List<LeaveRow> getLeaveRowByDate(@Param("queryDate") String queryDate);


    /**
     * 根据Id处理请假请求
     *
     * @param result    结果
     * @param requestId 请假记录ID
     * @return 操作受影响的记录数
     */
    @Update("update leave_row set check_flag = 1,agree_flag = #{result} where id = #{requestId}  ")
    Integer dealLeaveRequest(@Param("result") Integer result, @Param("requestId") Integer requestId);


    /**
     * 根据工号 日期 时间段 查询记录
     *
     * @param EmpNumber 员工号
     * @param leaveDate 日期
     * @return 记录数
     */
    @Select("select Count(id) from leave_row where emp_number = #{EmpNumber} AND leave_date = #{leaveDate}")
    Integer getLeaveRowByDateAndEmpNumberAndTime(@Param("EmpNumber") String EmpNumber, @Param("leaveDate") String leaveDate);


    @Select("select id,emp_number,emp_name,department,emp_position,leave_date,leave_time,reason,read_flag,check_flag,agree_flag " +
            "from leave_row where emp_number = #{empNumber } AND leave_date = #{queryDate}")
    List<LeaveRow> getEmpLeaveRowByDate(@Param("empNumber") String empNumber, @Param("queryDate") String queryDate);
}
